import 'dart:io';

import 'package:bwt_ad_flutter/ad/loader/BwtNativeAdLoader.dart';
import 'package:bwt_ad_flutter/ad/widget/nativeWidget/BwtNativeAdData.dart';
import 'package:bwt_ad_flutter/ad/widget/nativeWidget/BwtNativeAdWidget.dart';
import 'package:bwt_ad_flutter_example/util/Log.dart';
import 'package:bwt_ad_flutter_example/widget/SimpleWidget.dart';
import 'package:flutter/material.dart';

class NativeAdPage extends StatefulWidget {
  const NativeAdPage({Key? key}) : super(key: key);

  @override
  State<NativeAdPage> createState() => _NativeAdPageState();
}

class _NativeAdPageState extends State<NativeAdPage> {
  final BwtNativeAdLoader _nativeAdLoader = BwtNativeAdLoader();
  BwtNativeAdWidget? _nativeView;
  String adId = "";
  @override
  void initState() {
    super.initState();
    _nativeAdLoader.onNativeAdLoad = (bwtAd,BwtNativeAdData nativeAdData){
      //BwtNativeAdData 为自渲染数据
      print("title == ${nativeAdData.title}");
      print("imageUrl == ${nativeAdData.imageUrl}");
      print("iconImageUrl == ${nativeAdData.iconImageUrl}");
      setState(() {
        Log.d("加载成功");
        adId = bwtAd.adId;
        _nativeView = bwtAd.nativeAdview;
      });
    };
    _nativeAdLoader.onAdClick = (bwtAd) {
      Log.d("banner广告被点击");
    };

    _nativeAdLoader.onAdFailed = (msg) {
      Log.d("banner加载失败了吗$msg");
    };
    _nativeAdLoader.onAdExpose = (bwtAd) {
      Log.d("banner广告展示成功");
    };
    _nativeAdLoader.onAdClose = (bwtAd) {
      Log.d("banner广告关闭");
    };
  }
  //关闭广告
  closeAd(){
    _nativeAdLoader.closeAd(adId);
  }
  @override
  void dispose() {
    /// 释放广告
    _nativeAdLoader.release();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("信息流自渲染广告")),
      body: Center(
          child: Column(
        children: [
          SimpleWidget(
            margin: const EdgeInsets.only(top: 200),
            alignment: Alignment.center,
            width: 230,
            height: 48,
            decoration: BoxDecoration(
              color: Colors.blue,
              borderRadius: BorderRadius.circular(4),
            ),
            child: const Text(
              '加载信息流自渲染广告',
              style: TextStyle(
                color: Colors.white,
                fontSize: 16,
              ),
            ),
            onTap: () {
              _loadAd();
            },
          ),
          SimpleWidget(
            margin: const EdgeInsets.only(top: 50),
            alignment: Alignment.center,
            width: 230,
            height: 48,
            decoration: BoxDecoration(
              color: Colors.blue,
              borderRadius: BorderRadius.circular(4),
            ),
            child: const Text(
              '关闭广告',
              style: TextStyle(
                color: Colors.white,
                fontSize: 16,
              ),
            ),
            onTap: () {
              closeAd();
            },
          ),
          Visibility(
              key: ValueKey(_nativeView?.hashCode),
              child: Container(
                  margin: const EdgeInsets.only(top: 50), child: _nativeView)),
        ],
      )),
    );
  }

  _loadAd() {
    _nativeAdLoader.loadAd(
        posId: Platform.isAndroid ? "47228815" : "16785418",
        adWidth: 200,
        adHeight: 100);
  }
}
